package wifiunion.com.notice.widget.selectmemberdialog.adapter;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.util.List;

import wifiunion.com.notice.Constants;
import wifiunion.com.notice.R;
import wifiunion.com.notice.widget.selectmemberdialog.entity.Node;


public class SelectMemberTreeAdapter<T> extends SelectMemberListViewAdapter<T> {


    public SelectMemberTreeAdapter(ListView mTree, Context context, List<Node> datas/*, List<Node> visdatas*/)
            throws IllegalArgumentException, IllegalAccessException {
        super(mTree, context, datas/*, visdatas*/);
    }

    @Override
    public View getConvertView(final Node node, int position, View convertView, ViewGroup parent) {

        ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.selectmember_tree_list_item, parent, false);
            viewHolder = new ViewHolder();
            viewHolder.icon = (ImageView) convertView.findViewById(R.id.id_treenode_icon);
            viewHolder.headimg = (ImageView) convertView.findViewById(R.id.id_headimg);
            viewHolder.label = (TextView) convertView.findViewById(R.id.id_treenode_label);
            viewHolder.checkbox = (CheckBox) convertView.findViewById(R.id.id_treenode_cb);
            convertView.setTag(viewHolder);

        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        if (node.getIcon() == -1) {
            viewHolder.icon.setVisibility(View.INVISIBLE);
        } else {
            viewHolder.icon.setVisibility(View.VISIBLE);
            viewHolder.icon.setImageResource(node.getIcon());
        }

        viewHolder.checkbox.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                int checked = node.getChecked();

                switch (checked) {
                    case 0:
                        node.setChecked(1);
                        break;
                    case 1:
                        node.setChecked(0);
                        break;

                    default:
                        node.setChecked(0);
                        break;
                }
                refreshNodes(node);

            }
        });

        if (node.getChecked() == 1) {
            viewHolder.checkbox.setChecked(true);
        } else {
            viewHolder.checkbox.setChecked(false);
        }

        Drawable da /*= mContext.getResources().getDrawable(R.drawable.group_folder)*/;

        if (TextUtils.isEmpty(node.getPhone())) {
            da = mContext.getResources().getDrawable(R.drawable.group_folder);
            viewHolder.checkbox.setEnabled(true);
            if (node.getParent() == null) {
                viewHolder.checkbox.setVisibility(View.GONE);
            } else {
                viewHolder.checkbox.setVisibility(View.VISIBLE);
            }
            viewHolder.headimg.setVisibility(View.GONE);
            da.setBounds(0, 0, da.getIntrinsicWidth(), da.getIntrinsicHeight());
            viewHolder.label.setCompoundDrawables(da, null, null, null);
            viewHolder.label.setCompoundDrawablePadding(20);
            viewHolder.label.setText(node.getName());
        } else {
            viewHolder.checkbox.setVisibility(View.VISIBLE);
            viewHolder.headimg.setVisibility(View.VISIBLE);
            viewHolder.label.setCompoundDrawables(null, null, null, null);
            viewHolder.label.setCompoundDrawablePadding(0);
            viewHolder.label.setText(node.getName());
            Glide.with(mContext).load(Constants.HTTP_URL_IMAGESERVER + (node.getComparisonPic())).placeholder(R.drawable.device_location)
                    .error(R.drawable.device_location).centerCrop().into(viewHolder.headimg);
        }
        return convertView;
    }

    private final class ViewHolder {
        ImageView icon;
        ImageView headimg;
        TextView label;
        CheckBox checkbox;
    }

    public void checkedParent(Node node) {

        if (node.getParent() != null) {

            boolean allSelect = true;
            for (Node nn : node.getParent().getChildren()) {
                if (nn.getChecked() != 1) {
                    allSelect = false;
                }
            }
            if (allSelect) {
                Node pn = node.getParent();
                pn.setChecked(1);
                checkedParent(pn);
            }
        }

    }

    /**
     * @param node
     * @return void
     * @Name uncheckedParent
     * @Description 取消 选中 父项处理
     * @Author wifiunion
     * @Date 2017年6月5日 下午3:39:05
     */
    public void uncheckedParent(Node node) {

        if (node.getParent() != null) {
            Node pn = node.getParent();
            pn.setChecked(0);
            uncheckedParent(pn);
        }

    }

    /**
     * @param node
     * @return void
     * @Name refreshChild
     * @Description 选中 子项处理
     * @Author wifiunion
     * @Date 2017年6月5日 下午3:34:56
     */
    public void checkedChild(Node node) {
        if (node.getChildren() != null) {

            for (Node nn : node.getChildren()) {
                nn.setChecked(1);
                checkedChild(nn);
            }
        }
    }

    /**
     * @param node
     * @return void
     * @Name uncheckedChild
     * @Description 取消选中 子项处理
     * @Author wifiunion
     * @Date 2017年6月5日 下午3:38:37
     */
    public void uncheckedChild(Node node) {

        for (Node nn : node.getChildren()) {
            nn.setChecked(0);
            uncheckedChild(nn);
        }
    }

    /**
     * @param node
     * @return void
     * @Name refreshNodes
     * @Description TODO
     * @Author wifiunion
     * @Date 2017年6月5日 下午3:35:01
     */
    public void refreshNodes(Node node) {
        if (node.getChecked() == 1) {
            checkedParent(node);
            checkedChild(node);
        } else {
            uncheckedParent(node);
            uncheckedChild(node);
        }
        notifyDataSetChanged();
    }

}
